<?php
/* 
* @Title:  [前台通用控制器]
* @Author: {chenlei} [chenleib5@126.com]
* @Date:   2014-04-07 09:26:50
* @Last Modified by:   Administrator
* @Last Modified time: 2014-04-26 17:05:00
* @Copyright:  [hn7m.com]
*/
class CommonControl extends Control{
	private $chanModel=NULL;
	private $cateModel=NULL;
	private $userModel=NULL;

	public function __init(){
		$this->userModel=K('User');
		$uid=Q('session.uid',0,'intval');
		$uheadimg=$this->userModel->get_onedate_value($uid,'uheadimg');
		$this->assign('uheadimg',$uheadimg);//分配头像
		//分配栏目
		$this->chanModel=K('Channel');
		$chan=$this->chanModel->get_data();
		$this->assign('chan',$chan);
		//分配分类
		$this->cateModel=K('Category');
		$where=array('cpid'=>0,'cshow'=>1);  //顶级分类pid=0;
		$csort='csort ASC';  //按排序字段排序
		$field=array('cid','cpid','cname','clogo'); //筛选字段
		$category=$this->cateModel->get_data($field,$where,$csort);
		//重新组合数组,将子分类压到顶级分类中.成为三维数组
		foreach ($category as $k => $v) {
			$n = array('cpid'=>$v['cid']);
    		$category[$k]['down'] = $this->cateModel->get_data($field,$n,$csort);	
    	}
		$this->assign('category',$category);  //分配全部分类

		$topCate=$this->cateModel->get_data(array('cid','cname'),array('cpid'=>0,'cshow'=>1),$csort);
		$this->assign('topCate',$topCate);//只分配顶级分类
	}
	/**
	 * [session_to_cart 将$_SESSION['cart']数据压到数据库cart表中]
	 * @return [type] [description]
	 */
	public function session_to_cart($uid){
		//先删除该用户之前的旧数据
        K('Cart')->del_data($uid);
        //再填入数据
        if (isset($_SESSION['cart'])) {
        	$sessionData=$_SESSION['cart'];
        	K('Cart')->add_data($sessionData,$uid);
        }
	}
	/**
	 * [cart_to_session 购物车表数据压到$_SESSION['cart']]
	 * @return [type] [description]
	 */
	public function cart_to_session($uid){
		$cartData=K('Cart')->get_data(array('qm_user_uid'=>$uid));  //获得所有的数据
		if(!empty($cartData)){
			$goods=array();
			foreach ($cartData as $key => $value) {
				$options=explode(',', $value['soptions']);
				$options=array(
					'size'	=>$options[0],
					'color'	=>$options[1]
					);
				$gid=$value['qm_goods_gid'];
				//检查库存
				$actual_kucun=K('Goods')->get_onedate_value($gid,'ginventory');
				$now_kucun=$value['sinventory'];
				$buy_num=$value['snum'];

				//如果保存在购物车表中的库存数和现在商品表总的总库存不一样的话,证明库存数有变动,保存实际的库存数.
				$kucun=($now_kucun!=$actual_kucun)?$actual_kucun:$now_kucun;

				//如果购买的数量大于实际库存的话,证明库存不足了
				//实际上,这里应该再判断细一点,比如说,买3件但库存只有2件,应该告诉用户最多只能买两件,并给与提示,但这里,我统统给赋值为0,不让用户买了
				$num=($buy_num<=$actual_kucun)?$buy_num:0;
				$onetotal=($num==0)?0:$value['stotal'];
				if($num!=0){
					$gids[]=$gid;
				};
				$goods[$value['session_id']]=array(
					'id'	=>$gid,
					'name'	=>$value['sname'],
					'num'	=>$num,
					'price'	=>$value['sprice'],
					'place'	=>$value['splace'],
					'cate'	=>$value['scate'],
					'imgUrl'=>$value['simg_url'],
					'kucun'	=>$kucun,
					'bid'	=>$value['qm_combine_bid'],
					'options'=>$options,
					'total'	=>$onetotal
					);
			};
			$total_rows=K('Cart')->get_total('snum',array('qm_user_uid'=>$uid,'qm_goods_gid'=>$gids));	//获得购物车表中商品的总件数
			$total=K('Cart')->get_total('stotal',array('qm_user_uid'=>$uid,'qm_goods_gid'=>$gids));		//获得购物车表中的总价钱
			$_SESSION['cart']=array(
				'goods'			=>$goods,
				'total_rows'	=>$total_rows,
				'total'			=>$total
				);
		}
	}
}
?>